import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// element plus 按需导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // element plus
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [
        // 1.配置element采用sass样式
        ElementPlusResolver({ importStyle: "sass" })],
    }),

  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 自动导入定制化样式文件进行样式覆盖
        additionalData: `
          @use "@/styles/element/index.scss" as *;
          @use "@/styles/var.scss" as *;
        `
      }
    }
  },
  server: {
    host: 'localhost',
    port: 3000,
    //open: true,      // 是否自动在浏览器打开
    // 跨域配置
    proxy: {
      // 匹配所有 /demo 开头的路径
      '/demo': {
        // 目标路径
        target: 'http://loaclhost:5000',
        // 控制转发时的端口
        changeOrigin: true,
        // 重写路径，在转发时将 demo 换成 ''
        pathRewrite: { '^/demo': '' },
        // 是否支持 websocket
        ws: true
      }
    }
  }
})
